﻿/*
 * taskloader.cpp
 *
 *  Created on: 2018年2月14日
 *      Author: work
 */

#include <dm/export.hpp>

#define DM_API_OS_SYS DM_API_EXPORT

#include <dm/os/sys/taskloader.hpp>

#include <dm/os/sys/sysinfo.hpp>

#include <tinyxml.h>
#include <dm/env/env.hpp>
#include <dm/os/log/logger.hpp>

#include <dm/datetime.hpp>

namespace dm{
namespace os{
namespace sys{

static const char* logModule = "CTaskLoader.sys.os.dm";

/**
 * 加载任务信息
 * @param sysInfo
 */
CTaskLoader::CTaskLoader( CSysInfo* sysInfo,const int& taskSize ){
	if( taskSize<=0 ){
		log().info(THISMODULE "taskSize(%d) 不允许加载",taskSize);
	}else{
		TiXmlDocument doc(dm::env::getCfgDir()+"/task.xml");
		if( !doc.LoadFile() ){
			log().warnning(THISMODULE "不能加载任务配置文件task.xml");
		}else{
			TiXmlElement* elementDm = doc.FirstChildElement("dm");
			if( !elementDm ){
				log().warnning(THISMODULE "不能找到节点dm");
			}else{
				TiXmlElement* elementTask = elementDm->FirstChildElement("task");
				int i = 0;
				const char* temp;
				dm::os::sys::STaskInfo* info = (dm::os::sys::STaskInfo*)sysInfo->m_taskInfos;

				while( elementTask ){
					temp = elementTask->Attribute("program");
					if( !temp ){
						log().warnning(THISMODULE "任务%d没有指定program",i);
						continue;
					}

					info->program = temp;

					temp = elementTask->Attribute("parameters");
					if( temp ){
						dm::string::CStringRef r(temp);
						if( r.len()>STaskInfo::MaxLenOfParas ){
							log().warnning(THISMODULE "参数长度溢出%s",temp);
							continue;
						}

						int p = r.nextNoneChars(" \t");
						if( p<0 ){
							info->paras = "";
						}else
							info->paras = temp+p;

					}else
						info->paras = "";

					temp = elementTask->Attribute("desc");
					if( temp )
						info->desc = temp;
					else
						info->desc = "无";

					// 移除多余空格

					temp = elementTask->Attribute("start");
					if( temp ){
						if( strcmp(temp,"deny")==0 )
							info->startMode = info->SmDeny;
						else if( strcmp(temp,"daemon")==0 )
							info->startMode = info->SmDaemon;
						else if( strcmp(temp,"single")==0 )
							info->startMode = info->SmSingle;
						else if( strcmp(temp,"manual")==0 )
							info->startMode = info->SmManual;
						else if( strcmp(temp,"secondly")==0 )
							info->startMode = info->SmSecondly;
						else if( strcmp(temp,"minutely")==0 )
							info->startMode = info->SmMinutely;
						else if( strcmp(temp,"hourly")==0 )
							info->startMode = info->SmHourly;
						else if( strcmp(temp,"daily")==0 )
							info->startMode = info->SmDaily;
						else if( strcmp(temp,"monthly")==0 )
							info->startMode = info->SmMonthly;
						else if( strcmp(temp,"yearly")==0 )
							info->startMode = info->SmYearly;
						else if( strcmp(temp,"mondays")==0 )
							info->startMode = info->SmMondays;
						else if( strcmp(temp,"tuesdays")==0 )
							info->startMode = info->SmTuesdays;
						else if( strcmp(temp,"wednesdays")==0 )
							info->startMode = info->SmWednesdays;
						else if( strcmp(temp,"thursdays")==0 )
							info->startMode = info->SmThursdays;
						else if( strcmp(temp,"fridays")==0 )
							info->startMode = info->SmFridays;
						else if( strcmp(temp,"saturdays")==0 )
							info->startMode = info->SmSaturdays;
						else if( strcmp(temp,"sundays")==0 )
							info->startMode = info->SmSundays;
						else{
							log().error(THISMODULE "启动类型不正确%s",temp);
							continue;
						}
					}else{
						info->startMode = info->SmManual;
					}

					temp = elementTask->Attribute("delay");
					if( temp )
						info->delaySecs = atoi(temp);
					else
						info->delaySecs = 0;

					temp = elementTask->Attribute("time");
					if( temp ){
						if( !info->time.fromString(temp) ){
							log().error(THISMODULE "时间格式不正确%d",temp);
							continue;
						}
					}else{
						info->time = dm::CTimeStamp::cur();
					}

					elementTask = elementTask->NextSiblingElement("task");
					++i;
					if( i>=taskSize ){
						log().warnning(THISMODULE "缓冲区满%d",i);
						break;
					}

					++info;
				}

				sysInfo->m_info->taskCount = i;
			}
		}
	}
}

}
}
}
